//
//  BaseViewController.m
//  CBT
//
//  Created by 四点钟 on 2018/11/15.
//  Copyright © 2018 Mr.Zhang. All rights reserved.
//

#import "BaseViewController.h"

@interface BaseViewController (){
    JHUD *_hudView;
}
@property (nonatomic, assign) BOOL enableLoading;//支持加载中动画 手动设置为

@end

@implementation BaseViewController

- (void)dealloc{
    NSLog(@"[%@ dealloc]",[self class]);
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationController.navigationBar.hidden = NO;
    self.edgesForExtendedLayout = UIRectEdgeBottom;
    self.view.backgroundColor = UIColorFromRGB(0xf1f1f1);
    self.enableLoading = [self supportLoading];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(changeLanguage) name:ChangeLanguageNotificationName object:nil];
}

- (void)changeLanguage{}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    
    if ([[UIDevice currentDevice].systemVersion doubleValue] >= 10.0) {
        self.navigationController.navigationBar.subviews[0].subviews[1].hidden = YES;
    }

}

-(void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    [self.view endEditing:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)setEnableLoading:(BOOL)enableLoading{
    _enableLoading = enableLoading;
    
    if (!_hudView){
        _hudView = [[JHUD alloc] initWithFrame:self.view.bounds];
        _hudView.messageLabel.text = @"";
        _hudView.indicatorForegroundColor = UIColorFromRGBA(0x3C8BF6, 0.5);
        _hudView.indicatorBackGroundColor = [UIColor clearColor];
    }
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [self.view endEditing:YES];
}

- (BOOL)supportLoading{
    return NO;
}

- (void)startClearBgColorLoading{
    [self.view addSubview:_hudView];
    _hudView.backgroundColor = [UIColor clearColor];
    [_hudView showAtView:self.view hudType:JHUDLoadingTypeCircleJoin];
}

- (void)startLoading{
    [self.view addSubview:_hudView];
     _hudView.backgroundColor = UIColorFromRGBA(0xf1f1f1, 1);
    [_hudView showAtView:self.view hudType:JHUDLoadingTypeCircleJoin];
}
- (void)stopLoading{
    [_hudView hide];
}


//是否允许自动旋转
- (BOOL)shouldAutorotate {
    return NO;
}

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {
    return UIInterfaceOrientationPortrait;
}

- (BOOL)iphoneIsProfiledScreen {
    if (@available(iOS 11.0, *)) {
        CGFloat a =  [[UIApplication sharedApplication] delegate].window.safeAreaInsets.bottom;
        return a > 0;
    } else {
        return NO;
    }
}

- (void)jumpToLocalBrowserWithURL:(NSString *)url
{
    if ([NSString isBlankString:url]) {
        [HFStatusView showFailureMessage:kLocalizedString(@"敬请期待!",nil)];
    }
    else {
        [[UIApplication sharedApplication]openURL:[NSURL URLWithString:url] options:@{} completionHandler:^(BOOL success) {}];
    }
}

@end
